package com.iflytek.universitymanagement.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iflytek.universitymanagement.controller.QuestionController;
import com.iflytek.universitymanagement.domain.Question;
import com.iflytek.universitymanagement.mapper.QuestionMapper;
import com.iflytek.universitymanagement.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements QuestionService {

    private final QuestionMapper questionMapper;

    @Autowired
    public QuestionServiceImpl(QuestionMapper questionMapper) {
        this.questionMapper = questionMapper;
    }

    /**
     * 计算总分
     *
     * @param answers 用户提交的答案列表
     * @return 总分
     */
    @Override
    public int calculateTotalScore(List<QuestionController.AnswerRequest> answers) {
        int totalScore = 0;

        // 遍历每个用户的答案，计算总分
        for (QuestionController.AnswerRequest answer : answers) {
            Question question = questionMapper.selectById(answer.getTid());
            if (question != null) {
                // 假设 tAnswer 存储的是正确答案的ID，用户选择的答案与正确答案对比
                if (question.getTAnswer().equals(answer.getSelectedAnswer())) {
                    totalScore += question.getTScore();  // 正确时加上题目的分数
                }
            }
        }
        return totalScore;
    }
}
